package com.cloud.punish.workflow.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.punish.common.result.QkTreeNode;
import com.cloud.punish.common.result.Quokka;
import com.cloud.punish.common.utils.TreeHelper;
import com.cloud.punish.workflow.entity.WfActChoice;
import com.cloud.punish.workflow.service.WorkflowSearchService;

@RestController
@RequestMapping("/punish/api/workflow/search")
public class WorkflowSearchController {

	@Autowired
	private WorkflowSearchService workflowSearchService;

	@GetMapping("transit")
	public Quokka showTransit(long recId, long actId) {
		QkTreeNode[] transNode = workflowSearchService.searchTransitTreeNodes(recId, actId);
		List<WfActChoice> allChoices = workflowSearchService.listActTransChoise(actId);
		List<WfActChoice> choices = new ArrayList<WfActChoice>();
		for (WfActChoice c : allChoices) {
			for (QkTreeNode tran : transNode) {
				Map<String, Object> data = tran.getData();
				if (tran.isLeaf() && data.get("act_def_id").equals(c.getTargetActDefId())
						&& data.get("part_id").equals(c.getTargetPartId())) {
					choices.add(c);
					break;
				}
			}
		}
		Quokka result = new Quokka(true);
		result.getDataMap().put("trans", TreeHelper.buildList2Tree(Arrays.asList(transNode)));
		result.getDataMap().put("choices", choices);
		return result;
	}
	
	@GetMapping("rollback")
	public Quokka showRollback(long recId, long actId){
		return new Quokka(true, workflowSearchService.showRollback(recId, actId));
	}

}
